package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment1 extends Fragment implements ChatAdapter.OnChatClickListener {
    private RecyclerView recyclerView;
    private List<Chat> chatList;
    private ChatAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout1, container, false);

        recyclerView = view.findViewById(R.id.chatsRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        initChatList();

        adapter = new ChatAdapter(chatList, this);
        recyclerView.setAdapter(adapter);

        return view;
    }

    private void initChatList() {
        chatList = new ArrayList<>();
        // 使用不同的头像资源ID
        chatList.add(new Chat("张三", "今天天气真好", "上午 10:30", R.drawable.avatar1));
        chatList.add(new Chat("李四", "待会一起吃饭吗？", "上午 09:15", R.drawable.avatar2));
        chatList.add(new Chat("王五", "项目进展如何？", "上午 8:30", R.drawable.avatar3));
        chatList.add(new Chat("老板", "明天汇报一下项目", "昨天 20:30", R.drawable.avatar4));
        chatList.add(new Chat("李六", "今晚加个班", "昨天 19:30", R.drawable.avatar5));
        chatList.add(new Chat("弟弟", "今晚打原神", "昨天 16:30", R.drawable.avatar6));
        chatList.add(new Chat("哥哥", "上号打游戏", "昨天 14:30", R.drawable.avatar7));
        chatList.add(new Chat("姐姐", "好的", "昨天 12:30", R.drawable.avatar8));
    }

    @Override
    public void onChatClick(Chat chat) {
        Intent intent = new Intent(getActivity(), ChatActivity.class);
        intent.putExtra("chat_name", chat.getName());
        startActivity(intent);
    }
}